package www.wen.com.common.lib.helper

import android.content.Context
import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.view.popuview.TipPopupView

/**
 * @author WEN
 * @Description:
 * @date 2021/3/23 18:32
 */
object CreateXPopupHelper {

    /**
     * 创建显示结果的弹窗
     */
    fun createTipPopupView(entity: PopupViewEntity): TipPopupView {
        val popupView = XPopup.Builder(entity.context)
                .hasBlurBg(false)
                .dismissOnBackPressed(entity.isDismissOnBackPressed)
                .dismissOnTouchOutside(entity.isDismissOnTouchOutside)
                .isDestroyOnDismiss(true)
                .asCustom(
                        TipPopupView(
                                context = entity.context!!,
                                resId = entity.tipResId!!,
                                title = entity.tipTitle!!,
                                message = entity.tipMessage!!,
                                dismissCallBack = entity.tipDismissCallBack!!,
                        )
                )
        if (entity.isShow) {
            popupView.show()
        }
        popupView.delayDismissWith(entity.delay ?: 1500, entity.runnable)
        return popupView as TipPopupView
    }

}

class PopupViewEntity {


    /**
     * 公共参数
     */
    var context: Context? = null
    var autoDismiss: Boolean = true
    var isShow: Boolean = false
    var delay: Long? = null
    var runnable: Runnable? = null
    var isDestroyOnDismiss: Boolean = true
    var isDismissOnBackPressed: Boolean = true
    var isDismissOnTouchOutside: Boolean = true


    var key: Long? = null


    var type: Int? = null

    //BottomColumnNoPopupView
    var uiCallBack: UiCallBack? = null

    /**
     * BottomQrCodePopupView 的参数
     */
    var key3: Long? = null
    var is2Dcode: Boolean = true

    /**
     * StandardAlertPopupView 的参数
     */
    var standardAlertPopupViewTitle: String? = null
    var standardAlertPopupViewContent: String? = null
    var standardAlertPopupViewCancelContent: String = "取消"
    var standardAlertPopupViewConfirmContent: String = "删除"
    var cancelFunction: CancelFunction? = null
    var finishOnCancel: Boolean = true
    var confirmFunction: ConfirmFunction? = null

    /**
     * AlertPopupView 的参数
     */
    var alertContent: CharSequence? = null
    var alertCancelText: CharSequence? = null
    var alertConfirmText: CharSequence? = null
    var alertConfirmListener: OnConfirmListener? = null
    var alertCancelListener: OnCancelListener? = null

    /**
     * AffirmPopupView 的参数
     */
    var affirmContent: CharSequence? = null
    var affirmCancelText: CharSequence? = null
    var affirmConfirmText: CharSequence? = null
    var affirmConfirmListener: OnConfirmListener? = null
    var affirmCancelListener: OnCancelListener? = null

    /**
     * InformPopupView 的参数
     */
    var title: CharSequence = ""
    var content: CharSequence = ""
    var confirmContent: CharSequence = getString(R.string.confirm)

    /**
     * BottomSelectPopupView 的参数
     */
    var optionItems: List<String> = mutableListOf()
    var resultCallBackSelect: ResultUiCallBack<String>? = null


    var coroutineScope: CoroutineScope = MainScope()
    var lifecycleOwner: LifecycleOwner? = null
    var rfidCode: CharSequence = ""

    /**
     * ExceptionReasonPopupView 的参数
     */
    var reasonShowAtView: View? = null


    var resultCallBackVenderItemEntity: ResultUiCallBack<String>? = null




    /**
     * TipPopupView 所需要的参数
     */
    var tipResId: Int? = null
    var tipTitle: String? = null
    var tipMessage: String? = null
    var tipDismissCallBack: DismissCallBack? = null

    /**
     * BottomDatePopupView 所需要的参数
     */
    var bottomDateTimeList: List<Long>? = null
    var bottomDateTitle: CharSequence? = null
    var bottomDateResultCallBack: ResultUiCallBack<Long>? = null
    var bottomDateSelectOptionIndex: Long? = null

    /**
     * ShowResultPopupView 所需要的参数
     */
    var showResultTitle: String? = null
    var showResultOnlyShow: Boolean? = null
    var showResultResId: Int? = null
    var showResultOptionCount1: Int? = null
    var showResultOptionCount2: Int? = null
    var showResultOptionCount3: Int? = null
    var showResultUiCallBack: ResultUiCallBack<String>? = null


}
